import { fileURLToPath, URL } from 'node:url'
import { resolve } from "path";
import { defineConfig, loadEnv } from 'vite'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Unocss from 'unocss/vite'
import { presetUno, presetAttributify, presetIcons } from 'unocss'
export default defineConfig(({ command, mode }) => {
    let env = loadEnv(mode, process.cwd());
    return {
        server: {
            open: true,
            cors: true,
            host: env.VITE_APP_HOST,
            port: env.VITE_APP_PORT as unknown as number,
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    target: env.VITE_SERVE,
                    // changeOrigin: true,
                    rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ""),
                },
            },
        },
        plugins: [
            vue(),
            vueJsx(),
            vueSetupExtend({}),
            Unocss({
                presets: [
                    presetUno(),
                    presetAttributify(),
                    presetIcons()],
            }),
            // 使用 svg 图标
            createSvgIconsPlugin({
                iconDirs: [resolve(process.cwd(), "src/assets/svg")],
                symbolId: "icon-[dir]-[name]"
            }),
            AutoImport({
                imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        }
    }
})
